<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    *{
      margin: 0;
    }
      .tab{
        width: 100%;
        overflow: hidden;
      }
      .item{
        width: 33%;
        float: left;
        line-height: 30px;
        text-align: center;
        
      }
      .active{
        color: red;
      }
      .tab-border{
        width: 33%;
        height: 2px;
        background-color: red;
        transition: .3s;
      }
      .right-one{
        transform: translateX(125px);
      }
      .right-two{
        transform: translateX(250px);
      }
  </style>
</head>
<body>
    <div class="tab">
       <div class="item active">综合</div>
       <div class="item ">销量</div>
       <div class="item">价格</div>
    </div>
    <div class="tab-border"></div>
    <script>
      var item = document.getElementsByClassName('item');
      var active = document.getElementsByClassName('active')[0]
      var tb = document.getElementsByClassName('tab-border')[0];
      for (let i = 0; i < item.length; i++) {
          item[i].onclick = function(){
            var _this = this;
            var currentIndex = Array.from(item).indexOf(active);
              handerRange(currentIndex,i,tb);
              clearClassName();
              _this.className+=" active"
             
          }
      }
      function clearClassName(){
        for (let i = 0; i < item.length; i++) {
          item[i].className = "item"
        }
      }
      // 核心函数  @param1:当前下标; @param
      function handerRange(currentIndex,index,el){
          //判断当前位置
          var eleWidth = el.clientWidth;
          var cVal      = Math.abs(index - currentIndex);
          tb.style.transform = "translateX("+cVal * eleWidth +"px)";
      }
    
    
    </script>
</body>
</html>